package com.asdf.tests;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.asdf.application.NetServiceSimulator;
import com.asdf.external.stubs.VSDT;

public class mainTest
{
	static private String AIRPORT_SCHEME_FILE = "airports.xml";
	static private String CLUB_SCHEME_FILE = "clubs.xml";
	
	private NetServiceSimulator simulator = new NetServiceSimulator();
	
	private boolean _init(String airports, String clubs)
	{
		return simulator.init(airports, clubs); 
	}
	
	
	private String _subscribe(String id, String firstName,
		String lastName, String email, String address,
		boolean aviationClubMember, String aviationCompanyName,
		boolean getNewsLetters, String username, String ccNumber,
		String ccProvider, int ccExpirationMonth, int ccExpirationYear,
		int ccZIP)
	{
		VSDT vsdt = new VSDT();

		return simulator.subscribe(vsdt.encrypt(id),
			vsdt.encrypt(firstName),
			vsdt.encrypt(lastName),
			vsdt.encrypt(email),
			vsdt.encrypt(address),
			aviationClubMember,
			vsdt.encrypt(aviationCompanyName),
			getNewsLetters,
			vsdt.encrypt(username),
			vsdt.encrypt(ccNumber),
			vsdt.encrypt(ccProvider),
			ccExpirationMonth,
			ccExpirationYear,
			ccZIP);
	}
	
	private boolean _startConnection(String username, String pass)
	{
		VSDT vsdt = new VSDT();
		
		return simulator.startConnection(vsdt.encrypt(username),
			vsdt.encrypt(pass));
	}
	
	private boolean _startConnection( String username, String password,
			String ccNumber, String ccProvider, int ccExpirationMonth,
			int ccExpirationYear, int ccZIP)
	{
		VSDT vsdt = new VSDT();
		
		return simulator.startConnection(vsdt.encrypt(username),
			vsdt.encrypt(password),
			vsdt.encrypt(ccNumber),
			vsdt.encrypt(ccProvider),
			ccExpirationMonth,
			ccExpirationYear,
			ccZIP);
	}
	
	private boolean _endConnection(String username, int chargeTime)
	{
		VSDT vsdt = new VSDT();
		
		return simulator.endConnection( vsdt.encrypt( username), chargeTime);
	}
	
	@Test
	public void testSubscribeWithCC( )
	{	
		// Incorrect filename
		assertEquals(false, _init("asas",CLUB_SCHEME_FILE));
		assertEquals(true, _init(AIRPORT_SCHEME_FILE,CLUB_SCHEME_FILE));
		
		assertEquals("0@0", _subscribe("0", "name0", "lname0", "0@0",
			"addr0", true, "AFree", false, "0", "0", "0", 0, 0, 0));
		// Already exists
		assertEquals(null, _subscribe("0", "name0", "lname0", "0@0",
				"addr0", true, "AFree", false, "0", "0", "0", 0, 0, 0));
		// Illegal Card
		assertEquals(null, _subscribe("1", "name1", "lname1", "1@1",
			"addr1", false, null, false, "1", "1", "1", 0, 0, 1));
		assertEquals("1@1", _subscribe("1", "name1", "lname1", "1@1",
			"addr1", false, null, false, "1", "1", "1", 0, 0, 3));
		assertEquals("2@2", _subscribe("2", "name2", "lname2", "2@2",
			"addr2", false, null, true, "2", "2", "2", 0, 0, 0));
		// Illegal membership - non-exist company
		assertEquals(null, _subscribe("3", "name3", "lname3", "3@3",
				"addr3", true, "flot", true, "3", "3", "3", 0, 0, 0));
		// Illegal membership - non-member
		assertEquals(null, _subscribe("3", "name3", "lname3", "3@3",
				"addr3", true, "China Airlines", true, "3", "3", "3", 0, 0, 0));
		assertEquals("3@3", _subscribe("3", "name3", "lname3", "3@3",
			"addr3", true, "Aeroflot", true, "3", "3", "3", 0, 0, 0));
	}

	
	@Test
	public void test2( )
	{	
		// Incorrect filename
		assertEquals(false, _init("asas",CLUB_SCHEME_FILE));
		assertEquals(true, _init(AIRPORT_SCHEME_FILE,CLUB_SCHEME_FILE));
		
		assertEquals("0@0", _subscribe("0", "name0", "lname0", "0@0",
			"addr0", true, "AFree", false, "0", "0", "0", 0, 0, 0));
		assertEquals("1@1", _subscribe("1", "name1", "lname1", "1@1",
			"addr1", false, null, false, "1", "1", "1", 0, 0, 0));
		assertEquals("2@2", _subscribe("2", "name2", "lname2", "2@2",
			"addr2", false, null, true, "2", "2", "2", 0, 0, 0));
		assertEquals("3@3", _subscribe("3", "name3", "lname3", "3@3",
			"addr3", true, "Aeroflot", false, "3", "3", "3", 0, 0, 0));
		assertEquals("4@4", _subscribe("4", "name4", "lname4", "4@4",
				"addr4", true, "ABenLadenAir", true, "4", "4", "4", 0, 0, 0));
		
		simulator.switchAirport("0"); // Harare
		
		// Illegal password
		assertEquals(false, _startConnection("0", "a"));
		
		simulator.switchAirport("1"); // Bishkek
		
		assertEquals(true, _startConnection("0", "0@0"));
		
		simulator.switchAirport("0"); // Harare
		
		assertEquals(true, _startConnection("0", "0@0"));
		// Trying double connecting.
		assertEquals(false, _startConnection("0", "0@0"));
		assertEquals(true, _startConnection("1", "1@1", "0", "0", 0, 0, 0));

		simulator.switchAirport("-12"); // Illegal Airport
		assertEquals(false, _startConnection("0", "0@0"));
		assertEquals(false, _startConnection("1", "1@1", "0", "0", 0, 0, 0));
		
		simulator.switchAirport( "2"); // Ben-gurion
		// No such connection in the airport
		assertEquals(false, _endConnection( "0", 0));
		// No such connection in the airport + negative time
		assertEquals(false, _endConnection( "1", -213));
		// No such connection in the airport + never connected user
		assertEquals(false, _endConnection( "4", 0));
		// No such connection in the airport + non-exist user
		assertEquals(false, _endConnection( "7", 0));
		
		assertEquals(true, _startConnection("0", "0@0"));
		assertEquals(true, _endConnection( "0", 10));
		// Non-exist connection
		assertEquals(false, _endConnection( "0", 10));
		// Non-exist user
		assertEquals(false, _startConnection("7", "0@0"));

	}

}
